home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / examples / chap05 / ExampleServer.java < prev    next >
Encoding:
Java Source  |  1996-10-06  |  2.5 KB  |  81 lines

  1. // ExampleServer.java
  2. // Simple Server Application
  3. import java.net.*;
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. class ExampleServer{
  8.    public final static int PORT = 4130;
  9.  
  10.    public static void main(String[] args){
  11.       ServerSocket server_socket = null;
  12.       Socket client_socket = null;
  13.       DataInputStream in = null;
  14.       DataOutputStream out = null;
  15.       float x = 0.0f, y = 0.0f, z = 0.0f, r = 0.0f;
  16.  
  17.       System.out.println("Start server: " + PORT);
  18.  
  19.       // open socket on PORT
  20.       try{
  21.          server_socket = new ServerSocket(PORT);
  22.       } catch(IOException e){
  23.          System.out.println("Could not create socket on: " + PORT + ", " + e);
  24.          System.exit(1);
  25.       }
  26.  
  27.       System.out.println("Socket created: " + PORT);
  28.       System.out.println("Waiting for client...");
  29.  
  30.       // accept client's request
  31.       try{
  32.          client_socket = server_socket.accept();
  33.       } catch(IOException e) {
  34.          System.out.println("Accept failed: " + PORT + ", " + e);
  35.          System.exit(1);
  36.       }
  37.  
  38.       System.out.println("Connection established: " 
  39.                            + client_socket.getInetAddress());
  40.       System.out.println("Open input/output stream...");
  41.  
  42.       try{
  43.          in = new DataInputStream(client_socket.getInputStream());
  44.          out = new DataOutputStream(client_socket.getOutputStream());
  45.       } catch (IOException e) {
  46.          System.out.println("Could not create input/output stream on: " 
  47.                    + PORT + ", " + e);
  48.          System.exit(1);
  49.       }
  50.  
  51.       while(true){
  52.          System.out.println("Reading data from client...");
  53.          try {
  54.         r = in.readFloat();
  55.  
  56.             x = in.readFloat();
  57.             y = in.readFloat();
  58.             z = in.readFloat();
  59.             System.out.println("  rotation: " + r + " position: " + x + "," + y + "," + z);      
  60.          } catch (IOException e) {
  61.             System.out.println("Could not read data.");
  62.             System.exit(1);
  63.          }
  64.          try {
  65.             // caliculate a new position
  66.         x -= Math.sin(r);
  67.             z -= Math.cos(r); 
  68.  
  69.             out.writeFloat(x);
  70.             out.writeFloat(y);
  71.             out.writeFloat(z);
  72.             System.out.println("  Sending new position to client: " 
  73.                            + x + "," + y + "," + z);
  74.          } catch (IOException e) {
  75.             System.out.println("Could not write data");
  76.             System.exit(1);
  77.          }
  78.       }
  79.    }
  80. }
  81.